package com.tanhua.server.controller;


import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.server.CommentsService;
import com.tanhua.server.server.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    MovementsService movementsService;
    @Autowired
    private CommentsService commentsService;

    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(Movement movement,
                                    MultipartFile imageContent[] ) throws IOException {
       movementsService.movements(movement,imageContent);
       return ResponseEntity.ok(null);
    }


    /**
     * 我的动态
     */
    @GetMapping("/all")
    public ResponseEntity all(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer pagesize,
                              Long userId){
        PageResult pr = movementsService.findByuserId(userId,page,pagesize);
        //11111111
        return ResponseEntity.ok(pr);
    }

    /**
     * 好友动态
     */
    @GetMapping
    public ResponseEntity friend(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize){
         PageResult pr = movementsService.friend(page,pagesize);
         return ResponseEntity.ok(pr);
    }

    /**
     * 推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
            PageResult pr = movementsService.recommend(page,pagesize);
            return ResponseEntity.ok(pr);

    }

    /**
     * 查询单个ID
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity id(@PathVariable("id") String id){
        MovementsVo vo = movementsService.findById(id);
        return ResponseEntity.ok(vo);
    }

    /**
     * 点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.list(movementId);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 取消点赞
     * @param movementId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId){
        Integer all = commentsService.dislike(movementId);
        return ResponseEntity.ok(all);
    }

    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId){
         Integer all = movementsService.love(movementId);
         return ResponseEntity.ok(all);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId){
        Integer all = movementsService.unlove(movementId);
        return ResponseEntity.ok(all);
    }

    /**
     * 谁看过我
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> list = movementsService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }


}
